package com.sangeng.controller;


import com.sangeng.constant.enums.AppHttpCodeEnum;
import com.sangeng.domain.core.ResponseResult;
import com.sangeng.domain.dto.UserLoginDto;
import com.sangeng.domain.entity.User;
import com.sangeng.service.BlogLoginService;
import com.sangeng.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
public class LoginController {

    @Autowired
    private BlogLoginService blogLoginService;

    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody UserLoginDto userDto){
        if(!StringUtils.hasText(userDto.getUsername())){
            return ResponseResult.errorResult(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        User user = new User();
        user.setUserName(userDto.getUsername());
        user.setPassword(userDto.getPassword());
        return blogLoginService.login(user, true);
    }

    @GetMapping("/getInfo")
    public ResponseResult getInfo(){
        return blogLoginService.getInfo();
    }

    @GetMapping("/getRouters")
    public ResponseResult getRouters(){
        return blogLoginService.getRouters();
    }

    @PostMapping("/user/logout")
    public ResponseResult logout(){
        return blogLoginService.logout(true);
    }

}
